package com.mttang.sys.web.controller;

import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLDecoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import com.mttang.framework.core.web.controller.BaseDispatcherSimpleFormController;

public class ImageStreamView extends BaseDispatcherSimpleFormController{
    
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
    throws Exception {
        
        response.setContentType("image/jpg;charset=utf-8");
        String path= StringUtils.hasLength(request.getParameter("path"))?URLDecoder.decode(request.getParameter("path").trim(), "UTF-8"):null;
        
        try{
        if(path!=null){
            if (logger.isInfoEnabled()) {logger.info("path==================>"+path);}
            ServletOutputStream output=response.getOutputStream();
            InputStream in = new FileInputStream(path);
            byte tmp[] = new byte[256];
            
            int i=0;
            while((i=in.read(tmp))!=-1){
                output.write(tmp,0,i);
            }
            
            
            in.close();
            
            output.flush();
            output.close();
            
        }
        }catch(Exception e){
            if (logger.isInfoEnabled()) {logger.info("ERROR==================>"+e);}
        }
        return null;
    }
    
    

}
